﻿/**
 * 信息通知方式
 */
var pageSize = 3;
var pageNo = 1;
var temp = "";
var nameStust = 0;	//全局变量记录注册 名字状态
var descriptionStust = 0; //全局变量记录注册 描述说明状态
var changeNameReg = "";
var changeModelDescriptionReg = "";

$(function () {
    signsy();//初始化加载列表信息

    $("#queryMenu").click(function () {//条件查询菜单列表并分页
        signsy();
    });
    // 显示名称必填校验
    // $("#departmentDisplayName").blur(function () {
    //     if (!($("#departmentDisplayName").val().trim())) {
    //         $('.change-deptDisplayName-err').removeClass('hide').find("em").text('显示名称不能为空');
    //         $('.change-deptDisplayName-err').find("i").attr('class', 'icon-warn').css("color", "#ea5d5f");
    //         return;
    //     } else {
    //         $('.change-deptDisplayName-err').addClass("hide");
    //     }
    // });

    /*
     *修改科室信息
     */
    $("#submitMenu_update").click(function () {

        if (!($("#departmentName").val().trim())) {
            $('.change-deptName-err').removeClass('hide').find("em").text('科室名称不能为空');
            $('.change-deptName-err').find("i").attr('class', 'icon-warn').css("color", "#ea5d5f");
            return;
        } else {
            $('.change-deptName-err').addClass("hide");
        }

        var departmentId = $(this).attr("data-userId");
        var departmentName = $("#departmentName").val();
        var deptDisplayName = $("#departmentDisplayName").val();
        $.ajax({
            // url: "http://localhost:8080/sys/dept/update",
            url: "/sys/dept/update",
            type: "post",
            async: false,
            dataType: "json",
            data: {
                departmentId: departmentId,
                departmentName: departmentName,
                deptDisplayName: deptDisplayName,
                // departmentIntroduction: departmentIntroduction
            },
            success: function (data) {
                if (data.code == 1 && data.msg != null) {
                    $("#myModal_update").modal("hide");
                    signsy();
                }
                else{
                    alert("修改失败！"+data.msg);
                }
                /*
                 if(data.statemt==1)
                 {
                 // 成功
                 $("#queryMenu").click();
                 $('#changeModelClose').click()
                 }
                 else
                 {
                 //失败
                 alert(data.st);
                 }
                 */
            },
            error: function (data) {
                alert("请求超时！"+data.msg);
            }
        });
    });

   //点击新增
    $("#addDept").click(function () {
        //清空模态框
        var modal = $("#myModal_add");  //当前模态框
        var modalTag = modal.find('.form-control');
        for (var i = 0; i < modalTag.length; i++) {
            modalTag.eq(i).val("");
        }
    });

    /*
    *新增科室信息
    */
    $("#submitMenu_add").click(function () {
        // alert("新增");
        console.log("新增");

        // 点击提交再次校验
        if (!($("#addDepartmentId").val().trim())) {
            $('.change-deptId-err').removeClass('hide').find("em").text('科室ID不能为空');
            $('.change-deptId-err').find("i").attr('class', 'icon-warn').css("color", "#ea5d5f");
            return;
        } else {
            $('.change-deptId-err').addClass("hide");
        }
        if (!($("#addDepartmentName").val().trim())) {
            $('.change-deptName-err').removeClass('hide').find("em").text('科室名称不能为空');
            $('.change-deptName-err').find("i").attr('class', 'icon-warn').css("color", "#ea5d5f");
            return;
        } else {
            $('.change-deptName-err').addClass("hide");
        }

        var departmentId = $("#addDepartmentId").val();
        var departmentName = $("#addDepartmentName").val();
        var deptDisplayName = $("#addDepartmentDisplayName").val();
        $.ajax({
            url: "/sys/dept/add",
            // url: "http://localhost:8080/sys/dept/add",
            type: "post",
            async: false,
            dataType: "json",
            data: {
                departmentId: departmentId,
                departmentName: departmentName,
                deptDisplayName: deptDisplayName,
                // departmentIntroduction: departmentIntroduction
            },
            success: function (data) {
                if (data.code == 1 && data.msg != null) {
                    $("#myModal_add").modal("hide");
                    // alert("新增成功！");
                    signsy();
                }
                else{
                    alert("修改失败！"+data.msg);
                }
            },
            error: function (data) {
                alert("新增失败！"+data.msg);
            }
        });
    });

    /*
     *查询 通知方式信息
     */
    function signsy(page, pageSize, pageNo) {
        //得到查询条件
        var departmentName = $("#departmentName_search").val().trim();
        var departmentDisplayName = $("#departmentDisplayName_search").val().trim();
        $.ajax({
            // url : "data/departmentManagement.json",
            url: "/sys/dept/info",
            // url: "http://localhost:8080/sys/dept/info",
            type: "get",
            async: false,
            dataType: "json",
            data: {
                deptName: departmentName,
                displayName: departmentDisplayName
            },
            success: function (data) {
                var nothtml = "<tr><th class='index'>序号</th><th  class='departmentName'>科室名</th>" +
                    "<th class='departmentDisplayName'>显示名称</th><!--<th class='departmentIntroduction'>科室简介</th>--><th class='op'>操作</th></tr>";
                if (data.code == 1 && data.data.length) {
                    for (var i = 0; i < data.data.length; i++) {
                        nothtml += "<tr><td>" + (i + 1) + "</td>" +
                            "<td>" + data.data[i].departmentName + "</td><td>" + data.data[i].deptDisplayName + "</td>" +
                            /*"<td>"+data.data[i].departmentIntroduction+"</td>" +*/
                            "<td>" + "<a href=\"#\" data-toggle=\"modal\" data-target='#myModal_update' userId='" + data.data[i].departmentId + "' " + "class=\"btn btn-sm btn-primary operation allUpdate\">修改</a>&nbsp;" +
                            "<a href=\"#\" data-toggle=\"modal\" userId='" + data.data[i].departmentId + "' " +
                            "class=\"btn btn-sm btn-danger operation allDele\">删除</a></td>"+
                            "</tr>";
                    }
                    $("#TeamSignedUser").html(nothtml);

                    /*
                     *填充 修改模态框的数据
                     */
                    $(".allUpdate").click(function () {
                        // 点击修改清空模态框的标志
                        $('.change-deptDisplayName-err').removeClass('hide').find("em").text('');
                        $('.change-deptDisplayName-err').find("i").removeAttr('class', 'icon-ok');
                        $('.change-deptDisplayName-err').find("i").removeAttr('class', 'icon-warn');

                        // 填充数据
                        var tag = $(this).closest('tr').find('td');
                        var modal = $("#myModal_update");  //当前模态框
                        var modalTag = modal.find('.form-control');
                        modal.find('#submitMenu_update').attr("data-userId", this.id);

                        for (var i = 1, j = 0; i < tag.length; i++, j++) {
                            modalTag.eq(j).val(tag.eq(i).text());
                        }
                        // $("#departmentIntroduction").val(temp);
                    });
                    //   点击删除
                      $(".allDele").click(function () {
                          var pid=this.id;
                          // console.log("删除的科室id为:"+pid);
                          if (confirm("确认删除当前项？")) {
                              $.ajax({
                                  // url: "http://localhost:8080/sys/dept/delete",
                                  url: "/sys/dept/delete",
                                  type : "post",
                                  async : false,
                                  dataType : "json",
                                  data : {
                                      deptId:pid
                                  },
                                  success : function(data) {
                                      // console.log(data);
                                      // alert("删除成功！");
                                      signsy();
                                  },error:function(){
                                      alert("删除失败！");
                                  }
                              });
                          }
                      });
                    //赋值

                } else {
                    nothtml += "<tr><td colspan='6'>数据为空！！</td></tr>";
                    $("#TeamSignedUser").html(nothtml);
                    $("#total").html(0);
                    $("#current").html(0);
                }
            },
            error: function (error) {
                console.log(error);
            }
        });
    }


});
